<?php
//	ThinkphpHelper v0.3
//	
//	weiyunstudio.com
//	sjj zhuanqianfish@gmail.com
//	2014年9月18日
namespace TPH\Controller;

use Think\Controller;
use Think\Model;

class CRUDController extends Controller
{

    /**
     * curd操作页面
     */
    public function index()
    {
        $this->assign('tableNameList', getTableNameList());
        $this->assign('moduleNameList', getModuleNameList());
        $this->assign('db_prefix', C('DB_PREFIX'));
        $this->display();
    }

    public function getPageCode($tableName)
    {
        $Model = M($tableName); // 实例化对象
        $count = $Model->where('status=1')->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Model->where('status=1')->order('create_time')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show);
    }


    /**
     * 列出所有记录页面，读取并填充数据
     */
    public function previewAllPage()
    {
        $tableName = I('request.table');
        $templateFilePath = $this->getViewCode("index.html");
        $list = M($tableName)->limit(3)->select();
        $this->assign($tableName . 'List', $list);
        $resultCode = $this->fetch(null, $templateFilePath);
        echo $resultCode;
    }

    /**
     * 获取列表页代码
     */
    public function allPageCode()
    {
        echo $this->getViewCode("index.html");
    }

    /**
     * 生成新建前台页面代码
     */
    public function addPage()
    {
        echo $this->getViewCode("add.html");
    }

    public function editPage()
    {
        echo $this->getViewCode("edit.html");
    }

    /**
     * 列表页面
     */
    public function controllerFunctionIndexCode()
    {
        echo $this->getControllerCode(null, 'index.html');
    }

    /**
     * 添加页面
     */
    public function controllerFunctionAddCode()
    {
        echo $this->getControllerCode(null, 'add.html');
    }

    /**
     * 修改页面
     */
    public function controllerFunctionEditCode()
    {
        echo $this->getControllerCode(null, 'edit.html');
    }

    /**
     * 删除页面
     */
    public function controllerFunctionDelCode()
    {
        echo $this->getControllerCode(null, 'del.html');
    }


    /**
     * 生成所有代码对应的文件
     */
    public function createAllFiles()
    {
        $tableNameList = I('selectTableName'); // 控制器列表
        $moduleName = I('moduleName'); // 模块名称
        $isCURD = I('isCURD'); // 是否CURD方法代码
        $isModel = I('isModel'); // 是否创建模型
        $extendsModuleName = I('extendsModuleName') ? I('extendsModuleName') : ''; // 继承模块
        $extendsClassName = I('extendsClassName'); // 继承类对象
        $this->assign('extendsModuleName', $extendsModuleName);
        $this->assign('extendsClassName', $extendsClassName);
        $methodList = getDirList(MODULE_PATH . TPH_TEMPLATE_PATH . TPH_CONTROLLER_PATH . TPH_METHOD_PATH, 'file');
        foreach ($tableNameList as $tableName) {
            # 控制器
            $controllerStr = ""; // 定义控制器字符串
            if ($isCURD) {
                foreach ($methodList as $method) {
                    $controllerStr .= $this->getControllerCode(null, $method, $tableName, $moduleName) . "\r\n\r\n";
                }
            }
            $this->assign('allCode', $controllerStr);
            $controllerStr = $this->getControllerCode("Controller.html", null, $tableName, $moduleName);
            FileUtil::writeFile(getControllerPath($moduleName, $tableName), $controllerStr); //生成Controller文件
            # END-控制器

            # 模型
            if($isModel){
                $modelStr = "";
                $modelStr = $this->getModelCode("Model.html", $tableName, $moduleName);
                FileUtil::writeFile(getModelPath($moduleName, $tableName), $modelStr); //生成Controller文件
            }
            # END-模型

            # 视图
            $indexViewStr = $this->getViewCode("index.html", $tableName);
            $addViewStr = $this->getViewCode("add.html", $tableName);
            $editViewStr = $this->getViewCode("edit.html", $tableName);
            FileUtil::writeFile(getViewPath($moduleName, $tableName, 'index'), $indexViewStr);
            FileUtil::writeFile(getViewPath($moduleName, $tableName, 'add'), $addViewStr);
            FileUtil::writeFile(getViewPath($moduleName, $tableName, 'edit'), $editViewStr);
            # END-视图
        }

        echo "生成完成。";
    }


    /**
     * 获取控制器代码
     * @param $controllerFileName
     * @param null $methodFileName
     * @param null $tableName
     * @return string
     */
    public function getControllerCode($controllerFileName, $methodFileName = null, $tableName = null, $moduleName=null)
    {
        $this->getCodeInit();
        $tableName = $tableName ? $tableName : I('table');
        $isPage = I('isPage');
        $this->assign('tableName', columNameToVarName($tableName));
        $this->assign('moduleName', $moduleName);
        $this->assign('TableName', tableNameToModelName($tableName)); //修正为驼峰命名，首字母大写
        # 获取控制器模版代码
        $templateFilePath = MODULE_PATH . TPH_TEMPLATE_PATH . TPH_CONTROLLER_PATH . $controllerFileName;
        if ($methodFileName) {
            $templateFilePath = MODULE_PATH . TPH_TEMPLATE_PATH . TPH_CONTROLLER_PATH . TPH_METHOD_PATH . $methodFileName;
        }
        $this->assign('operateTitle', $operateTitle);
        $resultCode = $this->fetch($templateFilePath);
        # END-获取控制器模版代码
        return $resultCode;
    }

    /**
     * 获取模型代码
     * @param $controllerFileName
     * @param null $methodFileName
     * @param null $tableName
     * @return string
     */
    public function getModelCode($modelFileName, $tableName = null, $moduleName=null)
    {
        $this->getCodeInit();
        $tableName = $tableName ? $tableName : I('table');
        $this->assign('tableName', columNameToVarName($tableName));
        $this->assign('moduleName', $moduleName);
        $this->assign('TableName', tableNameToModelName($tableName)); //修正为驼峰命名，首字母大写
        # 获取控制器模版代码
        $templateFilePath = MODULE_PATH . TPH_TEMPLATE_PATH . TPH_MODEL_PATH . $modelFileName;
        $this->assign('operateTitle', $operateTitle);
        $resultCode = $this->fetch($templateFilePath);
        # END-获取控制器模版代码
        return $resultCode;
    }



    /**
     * 解析前台View模板
     * @param $templateFileName
     * @param $operateTitle
     * @param $content
     * @return mixed
     */
    public function getViewCode($templateFileName, $tableName = null)
    {
        $this->getCodeInit();
        $tableName = $tableName ? $tableName : I('table');
        $templateFilePath = MODULE_PATH . TPH_TEMPLATE_PATH . TPH_VIEW_PATH . $templateFileName;
        $TableName = tableNameToModelName($tableName);
        $tableInfoArray = getTableInfoArray($tableName);
        $tableArray = getTableArray($tableName);
        $columnNameKey = getColumnNameKey();
        $columnCommentKey = getColumnCommentKey();
        # 填充内容
        $this->assign('tableName', $tableName);
        $this->assign('TableName', $TableName);
        $this->assign('tableInfoArray', $tableInfoArray);
        $this->assign('tableArray', $tableArray);
        $this->assign('columnNameKey', $columnNameKey);
        $this->assign('columnCommentKey', $columnCommentKey);
        $fileContent = $this->fetch($templateFilePath);
        return $fileContent;
    }

    /**
     * 获取代码初始化
     */
    private function getCodeInit(){
        define('IS_EXTEND', false);
        define('IS_TEMPLATE_REPLACE', false);
    }

}